﻿define(['admin/app', 'service/admin/bazaarProductService'], function (app) {
    "use strict";
    app.controller('bazaarProductController', function ($scope, $state, $stateParams, $location, utils, w5cValidator, bazaarProductService, baseService) {
        $scope.list = {};
        $scope.query = { page: 1, size: 20 };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                bazaarProductService.gets($scope.query).success(function (response) { $scope.model = response.data; });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            getCategory: function () {
                bazaarProductService.getCategory().success(function (response) { $scope.categorys = response.data; });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    bazaarProductService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        $scope.model.category_id = response.data.category_id + '';
                    });
                } 
            },
            recommend: function (id, isRecommend) {
                var modal = utils.confirm({ msg: isRecommend ? "您确定要推荐该商品么" : "您确定要取消推荐该商品么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    bazaarProductService.recommend(id, isRecommend).success(function () {
                        methods.search();
                    });
                });
            },
            down: function (id) {
                var modal = utils.confirm({ msg:"确定要下架该商品么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    bazaarProductService.down(id).success(function () {
                        methods.search();
                    });
                });
            },
            audit: function (id, category_id, status, price, remark) {
                var modal = utils.confirm({ msg: "确定要审核么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    bazaarProductService.audit(id, Number(category_id), Number(status), price, remark).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.goBack();
                        }
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            goBack: function () {
                history.back();
            }
        };

        angular.extend($scope, methods);
        if ($state.includes('app.bazaarProduct.detail') || $state.includes('app.bazaarProduct.audit')) {
            methods.edit();
            if ($state.includes('app.bazaarProduct.audit')) {
                methods.getCategory();
            }
        } else {
            methods.search();
        }
    });
});